/****

    activequant - activestocks.eu

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

	
	contact  : contact@activestocks.eu
    homepage : http://www.activestocks.eu

****/
package org.activequant.core.domainmodel.account;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * book that gathers all orders, both filled and open. <br/>
 * Holds the following associated variables:
 * <ul>
 * <li>id(Long)</li>
 * <li>histories(List&lt;OrderHistory&gt;)</li>
 * </ul>
 * <br>
 * <b>History:</b><br>
 *  - [09.06.2007] Created (Ulrich Staudinger)<br>
 *  - [14.07.2007] Added persistence (Erik Nijkamp)<br>
 *  - [29.09.2007] Removed annotations (Erik Nijkamp)<br>
 *  - [01.11.2007] Added constructor (Erik Nijkamp)<br>
 *  - [16.12.2007] Major cleanup (Mike Kroutikov)<br>
 *
 *  @author Ulrich Staudinger
 */
public class OrderBook implements Iterable<OrderHistory>, Serializable {
	
	private static final long serialVersionUID = -8179548269004440110L;
	/**
	 * private Long id;
	 */
	private Long id;
	/**
	 * private List&lt;OrderHistory&gt; histories = new ArrayList&lt;OrderHistory&gt;();
	 */
	private List<OrderHistory> histories = new ArrayList<OrderHistory>();
	/**
	 * returns the associated id(Long)
	 * @return
	 */
	public Long getId() {
		return id;
	}
	/**
	 * sets the associated id(Long) with the given id(Long)
	 * @param id
	 */
	public void setId(Long id) {
		this.id = id;
	}
	/**
	 * returns whether the associated id(Long) is not <code>null</code>
	 * @return
	 */
	public boolean hasId() {
		return id != null;
	}
	/**
	 * returns an Iterator&lt;OrderHistory&gt; to the associated histories(List&lt;OrderHistory&gt;)
	 */
	public Iterator<OrderHistory> iterator() {
		return histories.iterator();
	}
	/**
	 * returns an OrderHistory[] array containing only those OrderHistory elements among the associated histories(List&lt;OrderHistory&gt;) whose completion(OrderCompletionEvent) is <code>null</code>
	 * @return
	 */
	public OrderHistory[] getOpenHistories() {
		List<OrderHistory> ticketList = new ArrayList<OrderHistory>();
		for (OrderHistory o : histories) {
			if (o.getHistoryCompletion() == null) {
				ticketList.add(o);
			}
		}
		return ticketList.toArray(new OrderHistory[0]);
	}
	/**
	 * returns an OrderHistory[] array containing only those OrderHistory elements among the associated histories(List&lt;OrderHistory&gt;) whose completion(OrderCompletionEvent) is not <code>null</code>
	 * @return
	 */
	public OrderHistory[] getClosedHistories() {
		List<OrderHistory> ticketList = new ArrayList<OrderHistory>();
		for (OrderHistory o : histories) {
			if (o.getHistoryCompletion() != null) {
				ticketList.add(o);
			}
		}
		return ticketList.toArray(new OrderHistory[0]);
	}
	/**
	 * returns an OrderHistory[] array containing only those OrderHistory elements among the associated histories(List&lt;OrderHistory&gt;) whose 
	 * completion(OrderCompletionEvent) is not <code>null</code> and has a non <code>null</code> terminalError(OrderEvent)
	 * @return
	 */
	public OrderHistory[] getCanceledOrRejectedHistories() {
		List<OrderHistory> historiesList = new ArrayList<OrderHistory>();
		for (OrderHistory o : histories) {
			if (o.getHistoryCompletion() != null && o.getHistoryCompletion().getTerminalError() != null) {
				historiesList.add(o);
			}
		}
		return historiesList.toArray(new OrderHistory[0]);
	}
	/**
	 * returns an OrderHistory[] array containing the associated histories(List&lt;OrderHistory&gt;)
	 * @return
	 */
	public OrderHistory[] getHistories() {
		return histories.toArray(new OrderHistory[] {});
	}
	/**
	 * adds the given history(OrderHistory) to the associated histories(List&lt;OrderHistory&gt;)
	 * @param history
	 */
	public void addHistory(OrderHistory history){
		this.histories.add(history);
	}
}
